package cn.yunhe.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.yunhe.controller.ListController;
import cn.yunhe.entity.Listuser;
import cn.yunhe.entity.Page;
import cn.yunhe.entity.ShowList;

import com.alibaba.fastjson.JSON;

public class OprAction {
	private HttpServletRequest request=ServletActionContext.getRequest();
	private HttpServletResponse response=ServletActionContext.getResponse();
	private static Logger logger = LogManager.getLogger(OprAction.class);
	@Autowired
	private ListController lc;
	
	/**
	 * 查询所有信息
	 * @return
	 */
//	public String showAll(){
//		List<ShowList> list=lc.showAll();
//		request.setAttribute("list", list);
//		return "success";
//	}
	
	/**
	 * 分页查询所有信息
	 * @return
	 * @throws IOException 
	 */
	public void showAll() throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		Page page=new Page();
		Integer index=1;
		String i=request.getParameter("index");
		if(i!=null){
			index=Integer.parseInt(i);
		}
		Integer pageSize=5;
		page.setPageIndex(index);
		page.setPageSize(pageSize);
		page.setDataTotal(lc.showAll().size());
		HashMap map=new HashMap();
		map.put("minIndex", (index-1)*pageSize);
		map.put("maxIndex", (index)*pageSize);
		List<ShowList> list=lc.showAllByPage(map);
		Listuser user=(Listuser)request.getSession().getAttribute("user");
		if(list.size()>0){
			logger.info("成功获取当前页数据,当前操作用户:"+user.getUsername());
		}else{
			logger.warn("获取当前页数据失败,当前操作用户:"+user.getUsername());
		}
		page.setPageList(list);
		out.print(JSON.toJSONString(page));
		out.flush();
		out.close();
	}
	
	/**
	 * 根据id查找相对应的数据，并放入request范围中
	 * @return
	 * @throws IOException 
	 */
	public String searchById() throws IOException{
		
		response.setCharacterEncoding("utf-8");
		Integer lid=Integer.parseInt(request.getParameter("lid"));
		String opr=request.getParameter("opr");
		ShowList item=lc.searchById(lid);
		Listuser user=(Listuser)request.getSession().getAttribute("user");
		if(item!=null){
			logger.info("查找当前信息成功,当前操作用户:"+user.getUsername());
			if(opr!=null){//不为空表示是编辑操作
				request.setAttribute("edititem", item);
				return "edit";
			}//为空表示查询操作
			request.setAttribute("item", item);
			return "success";
		}else{
			logger.warn("查找当前信息失败,当前操作用户:"+user.getUsername());
			return "fail";
		}
	}
	/**
	 * 获取填写的每条数据，并更新数据库，更新成功返回1
	 * @return
	 */
	public String edit(){
		//获取每个表单元素的值
		Integer lid=Integer.parseInt(request.getParameter("lid"));
		String lname=request.getParameter("lname");
		String lpwd=request.getParameter("lpwd");
		String laddress=request.getParameter("laddress");
		ShowList item=new ShowList();
		item.setLid(lid);
		item.setLname(lname);
		item.setLpwd(lpwd);
		item.setLaddress(laddress);
		HashMap map=new HashMap();
		map.put("lid", item.getLid());
		map.put("lname", item.getLname());
		map.put("lpwd", item.getLpwd());
		map.put("laddress", item.getLaddress());
		int i=lc.edit(map);//执行更新操作
		Listuser user=(Listuser)request.getSession().getAttribute("user");
		if(i>0){
			logger.info("更新操作成功,当前操作用户:"+user.getUsername());
			ShowList retitem=lc.searchById(lid);
			request.setAttribute("edititem", retitem);
			return "success";
		}else{
			logger.warn("更新操作失败,当前操作用户:"+user.getUsername());
			return "fail";
		}
	}
	
	/**
	 * 删除操作
	 * @throws IOException 
	 */
//	public void del() throws IOException{
//		response.setCharacterEncoding("utf-8");
//		PrintWriter out=response.getWriter();
//		Integer lid=Integer.parseInt(request.getParameter("lid"));
//		lc.del(lid);
//		List<ShowList> list=lc.showAll();
//		out.print(JSON.toJSONString(list));
//		out.flush();
//		out.close();
//	}
	
	/**
	 * 删除后显示分页后的结果集
	 * @throws IOException
	 */
	public void del() throws IOException{
		response.setCharacterEncoding("utf-8");
		Integer lid=Integer.parseInt(request.getParameter("lid"));
		int i=lc.del(lid);
		Listuser user=(Listuser)request.getSession().getAttribute("user");
		if(i>0){
			logger.info("删除操作成功,当前操作用户:"+user.getUsername());
		}else{
			logger.warn("删除操作失败,当前操作用户:"+user.getUsername());
		}
	}
	
	/**
	 * 翻页查询
	 */
//	public void paging() throws IOException{
//		response.setCharacterEncoding("utf-8");
//		PrintWriter out=response.getWriter();
//		Page page=new Page();
//		Integer index=1;
//		String i=request.getParameter("index");
//		if(i!=null){
//			index=Integer.parseInt(i);
//		}
//		Integer pageSize=5;
//		page.setPageIndex(index);
//		page.setPageSize(pageSize);
//		page.setDataTotal(lc.showAll().size());
//		HashMap map=new HashMap();
//		map.put("minIndex", (index-1)*pageSize);
//		map.put("maxIndex", (index)*pageSize);
//		List<ShowList> list=lc.showAllByPage(map);
//		page.setPageList(list);
//		request.setAttribute("list", page);
//		out.print(JSON.toJSONString(page));
//		out.flush();
//		out.close();
//	}
}
